<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>snsapi_userinfo</title>
<script type="text/javascript" src="./js/jquery.min.js"></script>
</head>
<body>
本页面用于显式授权测试<br><br>
url:
<div id="a">

</div>


<script type="text/javascript">
var appid="wxc408c0125834275f";
var url=encodeURIComponent(location.href.split("#")[0]);
var scope="snsapi_userinfo"; //snsapi_userinfo 
var param=getParams(location.search.substr(1));
alert(url);
function getParams(params){
	var list=params.split("&");
	var result={};
	for(index in list){
		var kv=list[index].split("=");
		result[kv[0]]=kv[1];
	}
	return result;
}

document.ready = function () {
	if(!param["code"]){
		var codeUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+url+"&response_type=code&scope="+scope+"&state=STATE#wechat_redirect";
		location.href=codeUrl;
	}
	else{
		var baseUrl="wx/getOpenid.zx?code="+param["code"];
		req("GET",baseUrl,"","json",function(data){
			for(key in data){
				$("#a").append(key+":"+data[key]+"<br>");
			}
			if(data.errcode==undefined){
					console.log(data);
					var requestUrl="wx/getUserInfo.zx?code="+param["code"]+"&access_token="+data.access_token;
					req("GET",requestUrl,"","json",function(data){
							if(data.errcode==undefined){
								for(key in data){
									$("#a").append(key+":"+data[key]+"<br>");
								}
							}
					});
				}
		});
		
	}
}
document.ready();

var xmlhttp;
//type:POST OR GET ,Async true:同步方法,false:异步方法
function req(type,url,RequestJson,dataType,CallbackMethod)
{
 $.ajax({
        type: type,
        url: url,
        data: RequestJson,
        dataType: dataType,
        Async:true,
        success: CallbackMethod
    });
}
</script>
</body>
</html>